import 'package:flutter/material.dart';
import 'package:junyeyishu/utils/utils.dart';

class QGSaleTypeSwitchWidget extends StatefulWidget {
  int current;
  List<String> items;
  List<int> nums;
  MainAxisAlignment alignment;
  Function(int index) onSwitch;
  bool subline;
  bool underline;

  double fontSize;
  double maxFontSize;

  QGSaleTypeSwitchWidget(
      {Key? key,
      required this.current,
      required this.items,
      required this.nums,
      required this.onSwitch,
      this.fontSize = 14,
      this.maxFontSize = 14,
      this.subline = false,
      this.underline = false,
      this.alignment = MainAxisAlignment.spaceBetween})
      : super(key: key);

  @override
  State<QGSaleTypeSwitchWidget> createState() => _QGSaleTypeSwitchWidgetState();
}

class _QGSaleTypeSwitchWidgetState extends State<QGSaleTypeSwitchWidget> {
  @override
  Widget build(BuildContext context) {
    List<Widget> children = [];
    for (int i = 0; i < widget.items.length; i++) {
      var child = GestureDetector(
        onTap: () {
          setState(() {});
          widget.onSwitch(i);
        },
        child: Stack(
          children: [
            Visibility(
              visible: widget.underline && i == widget.current,
              child: Positioned(
                  bottom: 0,
                  left: 0,
                  right: 0,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Container(
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.only(
                                topLeft: Radius.circular(50.rpx),
                                topRight: Radius.circular(50.rpx)),
                            color: const Color(0xFF111111)),
                        width: 34.rpx,
                        height: 4.rpx,
                      ),
                    ],
                  )),
            ),
            Container(
              color: Colors.transparent,
              padding: EdgeInsets.only(
                  top: 20.rpx, bottom: 15.rpx, left: 20.rpx, right: 20.rpx),
              child: Stack(
                children: [
                  Visibility(
                    visible: widget.subline && i == widget.current,
                    child: Positioned(
                      left: 0,
                      right: 0,
                      bottom: 0,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Container(
                            width: 34.rpx,
                            height: 4.rpx,
                            decoration: BoxDecoration(
                                borderRadius: BorderRadius.only(
                                    topLeft: Radius.circular(50.rpx),
                                    topRight: Radius.circular(50.rpx)),
                                color: const Color(0xFF111111)),
                          ),
                        ],
                      ),
                    ),
                  ),
                  Column(
                    children: [
                      Text(
                        widget.items[i],
                        style: TextStyle(
                            color: (i == widget.current)
                                ? const Color(0xFF111111)
                                : const Color.fromRGBO(17, 17, 17, 0.5),
                            fontSize: (i == widget.current)
                                ? toFit(widget.maxFontSize)
                                : toFit(widget.fontSize),
                            fontWeight: (i == widget.current)
                                ? FontWeight.bold
                                : FontWeight.normal),
                      ),
                      // SizedBox(height: 4.rpx),
                      // Text(
                      //   "${widget.nums[i]}单",
                      //   style: TextStyle(
                      //       color: (i == widget.current)
                      //           ? const Color(0xFF111111)
                      //           : const Color(0xFF6D6D6C),
                      //       fontSize: 10.rpx,
                      //       fontWeight: (i == widget.current)
                      //           ? FontWeight.bold
                      //           : FontWeight.normal),
                      // ),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      );
      children.add(child);
    }

    return Row(mainAxisAlignment: widget.alignment, children: children);
  }
}
